const config=require("./config.js")
App({
  onLaunch: function() {
    
    // 一、获取用户信息
    wx.getSetting({
      success: res => {
        console.info("1.........wx.getSetting.....")

        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.info("2.........wx.getUserInfo.....")
              console.info("scope.userInfo：" + JSON.stringify(res));
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo
              this.globalData.request_common_para.nickName = res.userInfo.nickName
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }

              // 登录
              wx.login({
                success: res => {
                  console.info("4.........wx.login.....")
                  console.info("wx.login:  " + JSON.stringify(res))
                  wx.request({
                    url: this.getHost()+'/datalake/v1/userManages/codeNew/' + res.code,
                    success: res => {
                      if (res.statusCode == 200) {
                        
                        console.info(this.getHost()+"/datalake/v1/userManages/codeNew/ result value:" + JSON.stringify(res));

                        this.globalData.request_common_para.openid = res.data.openid
                        /**根据openid查詢user對象 */
                        console.info("this.globalData.request_common_para:" + JSON.stringify(this.globalData.request_common_para))
                        if (this.globalData.request_common_para.openid) {
                          wx.request({
                            url:this.getHost()+"/datalake/v1/userInfo?openId=" + this.globalData.request_common_para.openid,
                            method: "GET",
                            success: res => {
                              console.info("userinfo get success:" + JSON.stringify(res))
                              if (res.statusCode == 200 && res.data && res.data.length > 0) {
                                this.globalData.request_common_para.userInfoId = res.data[0].userInfoId;
                                this.globalData.request_common_para.customer_name = res.data[0].customer_name;
                                this.globalData.request_common_para.nickName = res.data[0].nick_name;
                                this.globalData.request_common_para.roleName = res.data[0].type
                                this.globalData.request_common_para.store_name = res.data[0].store_name
                                let menuList =[]
                                if (res.data[0].menu){
                                  menuList = res.data[0].menu.split(",");
                                } 
                                this.globalData.menuListData = [];
                                let menuListData = [];
                                
                                for (let i in menuList) {
                                  if (menuList[i] == "客户需求") {
                                    let data = {
                                      "id": i,
                                      "name": "客户需求",
                                      "url": "/page/cust/custInfoList"
                                    }
                                    menuListData.push(data)
                                  }
                                  if (menuList[i] == "需求审核") {
                                    let data = {
                                      "id": i,
                                      "name": "需求审核",
                                      "url": "/page/cust/custInfoAuditing"

                                    }
                                    menuListData.push(data)
                                  }
                                  if (menuList[i] == "需求汇总") {
                                    let data = {
                                      "id": i,
                                      "name": "需求汇总",
                                      "url": "/page/cust/custInfoStatistics"
                                    }
                                    menuListData.push(data)
                                  }
                                  if (menuList[i] == "成交战败") {
                                    let data = {
                                      "id": i,
                                      "name": "成交战败",
                                      "url": "/page/cust/custListFinish"
                                    }
                                    menuListData.push(data)
                                  }
                                }
                                this.globalData.menuListData = menuListData
                                this.gotNext()

                              } else if (res.statusCode == 500) {
                                let data = {};
                                data.nickName = this.globalData.request_common_para.nickName
                                data.openId = this.globalData.request_common_para.openid
                                wx.request({
                                  url:this.getHost()+ '/datalake/v1/userInfo ',
                                  data: data,
                                  method: "POST",
                                  header: {
                                    'content-type': 'application/json' // 默认值
                                  },
                                  success: res => {
                                    console.info("create userInfo succes:" + JSON.stringify(res))
                                    this.globalData.request_common_para.userInfoId = res.data.userInfoId;
                                    this.globalData.request_common_para.customer_name = res.data.customer_name;
                                    this.globalData.request_common_para.nickName = res.data.nick_name;
                                    this.globalData.request_common_para.roleName = res.data.type
                                    let menuList =[]
                                    if (res.data.menu){
                                      menuList = res.data.menu.split(",");
                                    } 
                                    this.globalData.menuListData = [];
                                    let menuListData = [];
                                    for (let i in menuList) {
                                      if (menuList[i] == "客户需求") {
                                        let data = {
                                          "id": i,
                                          "name": "客户需求",
                                          "url": "/page/cust/custInfoList"
                                        }
                                        menuListData.push(data)
                                      }
                                      if (menuList[i] == "需求审核") {
                                        let data = {
                                          "id": i,
                                          "name": "需求审核",
                                          "url": "/page/cust/custInfoAuditing"

                                        }
                                        menuListData.push(data)
                                      }
                                      if (menuList[i] == "需求汇总") {
                                        let data = {
                                          "id": i,
                                          "name": "需求汇总",
                                          "url": "/page/cust/custInfoStatistics"
                                        }
                                        menuListData.push(data)
                                      }
                                      if (menuList[i] == "成交战败") {
                                        let data = {
                                          "id": i,
                                          "name": "成交战败",
                                          "url": "/page/cust/custListFinish"
                                        }
                                        menuListData.push(data)
                                      }
                                    }
                                    this.globalData.menuListData = menuListData
                                    
                                    this.gotNext()

                                  },
                                  fail: function(res) {
                                    console.error("create userInfo err:" + JSON.stringify(res));
                                  }
                                })
                              }
                            },
                            fail: function(res) {
                              console.info(" userinfo get fail:" + res)
                            }

                          })
                        }
                      }
                    },
                    fail: res => {}
                  })
                }
              })

              this.intiSysConfigData();
              this.getBankListData()
            }
          })
        }
      }
    })
    //二、获取系统消息
    wx.getSystemInfo({
      success: res => {
        this.globalData.request_common_para.brand = res.brand
        this.globalData.request_common_para.model = res.model
        this.globalData.request_common_para.version = res.version
        this.globalData.request_common_para.system = res.system
        this.globalData.request_common_para.platform = res.platform
      }
    })


    
    wx.request({
      url: this.getHost() + "/datalake/v1/brandMakes/all",
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        console.info(new Date())
        if (res.statusCode == 200) {
          console.info(res.data)
          wx.setStorage({
            key: "carTypeList",
            data: res.data,
            success:function(res){
              console.info(res)
            }
          })    
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    request_common_para: {},
    storeListData: [],  //门店列表
    bankListData:[],//银行列表
    custSourceListData: [{
      name: "二手车之家",
      value: "二手车之家"
    }, {
      name: "朋友介绍",
      value: "朋友介绍"
    }, {
      name: "自己到店",
      value: "自己到店"
    }, {
      name: "保卖车",
      value: "保卖车"
    }, {
      name: "淘车",
      value: "淘车"
    }, {
      name: "其他",
      value: "其他"
    }],
    custStatuListData: [{
      name: "已付订金",
      value: "已付订金"
    }, {
      name: "非常强烈",
      value: "非常强烈"
    }, {
      name: "强烈",
      value: "强烈"
    }, {
      name: "一般",
      value: "一般"
    }, {
      name: "已成交",
      value: "已成交"
    }, {
      name: "战败",
      value: "战败"
    }],
    yearListData: [{
      name: "0-1年",
      value: "0-1年"
    }, {
      name: "1-3年",
      value: "1-3年"
    }, {
      name: "3-5年",
      value: "3-5年"
    }, {
      name: "5-8年",
      value: "5-8年"
    }, {
      name: "8-10年",
      value: "8-10年"
    }, {
      name: "10年以上",
      value: "10年以上"
    }],
    mileageListData: [{
      name: "0-1万公里",
      value: "0-1万公里"
    }, {
      name: "1-3万公里",
      value: "1-3万公里"
    }, {
      name: "3-5万公里",
      value: "3-5万公里"
    }, {
      name: "5-10万公里",
      value: "5-10万公里"
    }, {
      name: "10万公里以上",
        value: "10万公里以上"
    }],
    colorListData: [{
      name: "黑",
      value: "黑"
    }, {
      name: "白",
      value: "白"
    }, {
      name: "银",
      value: "银"
    }, {
      name: "灰",
      value: "灰"
    }, {
      name: "红",
      value: "红"
    }, {
      name: "蓝",
      value: "蓝"
    }, {
      name: "绿",
      value: "绿"
    }, {
      name: "黄",
      value: "黄"
    }, {
      name: "金",
      value: "金"
    }, {
      name: "棕",
      value: "棕"
    }],
    menuListData: [],
    cityListData: ["上海", "北京","南通","天津","武汉"],
    carNoRegionList: [{
      name: "沪A",
      value: "沪A"
    }, {
      name: "沪C",
      value: "沪C"
    }, {
      name: "外地",
      value: "外地"
    }]
  },
  toast1Tap: function() {
    wx.showToast({
      title: "成功"
    })
  },
  toast3Tap: function() {
    wx.showToast({
      title: "loading",
      icon: "loading",
      duration: 1000
    })
  },
  toast4Tap: function (e) {
    wx.showToast({
      title: e,
      icon: "success",
      duration: 1000
    })
  },
  noTitlemodalTap: function(e) {
    wx.showModal({
      content: e,
      showCancel: false,
      confirmText: "确定",
      cancelText: "取消"
    })
  },
  onHide: function() {
    wx.clearStorage()
    wx.clearStorageSync()
  },
  saveFormId:function(form_id) {
    let data = {}
    data.form_id = form_id
    data.status = 0
    data.open_id = this.globalData.request_common_para.openid
    wx.request({
      url: this.getHost()+'/datalake/v1/formIds',
      data: data,
      method: "POST",
      success: res => {},
      fail: res => {
        console.error(this.getHost()+"/datalake/v1/formIds err:" + JSON.stringify(res))
      },
      complete: res => {

      }
    })
  },
  intiSysConfigData:function(){
    wx.request({
      url: this.getHost()+'/datalake/v1/params/key',
      method: "POST",
      data: {
        "key": "客户来源"
      },
      success: res => {
        if (res.data.type == "客户来源") {
          let cityArr = res.data.value.split(";")
          let custSourceListData = []
          for (let i in cityArr) {
            var data = {
              "name": cityArr[i],
              "value": cityArr[i]
            }
            custSourceListData.push(data)
          }
          this.globalData.custSourceListData = custSourceListData
        }
      }
    })

    wx.request({
      url: this.getHost() + '/datalake/v1/params/key',
      method: "POST",
      data: {
        "key": "门店来源"
      },
      success: res => {
        if (res.data.type == "门店来源") {
          let cityArr = res.data.value.split(";")
          let storeListData = ["---请选择---"]
          for (let i in cityArr) {
            var data = {
              "name": cityArr[i],
              "value": cityArr[i]
            }
            storeListData.push(cityArr[i])
          }
          this.globalData.storeListData = storeListData
        }
      }
    })
    
  },
  getBankListData:function(){
    wx.request({
      url: this.getHost() + '/datalake/v1/params/key',
      method: "POST",
      data: {
        "key": "银行列表"
      },
      success: res => {
        console.info(JSON.stringify(res.data))
        if (res.data.type == "银行列表") {
          let cityArr = res.data.value.split(";")
          let bankListData = ["---请选择---"]
          
          for (let i in cityArr) {
            var data = {
              "name": cityArr[i],
              "value": cityArr[i]
            }
            bankListData.push(cityArr[i])
          }
          this.globalData.bankListData = bankListData
        }
      }
    })
  },
  gotNext:function() {
    wx.navigateTo({
      url: '/page/index/index',
    });
  },
  getHost:function(){
    return config.host;
  }
})